var express = require('express');
var path = require('path')
var app = express();
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var mysql = require('mysql');
var pool = mysql.createPool({
    host: '47.106.159.59',
    user: 'root',
    password: 'jiemov1612',
    database: 'jiemov',
    port: 3306
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
    res.header("Content-Type",' application/json;charset=utf-8');
    if(req.method=="OPTIONS") res.send(200);/*让options请求快速返回*/
    else  next();
});

var fs = require('fs');
var http = require('http');
var https = require('https');
var privateKey  = fs.readFileSync("C:/inetpub/wwwroot/httpsacme-v01.api.letsencrypt.org/jiemov.cn-key.pem", 'utf8');
var certificate = fs.readFileSync("C:/inetpub/wwwroot/httpsacme-v01.api.letsencrypt.org/jiemov.cn-crt.pem", 'utf8');
var credentials = {key: privateKey, cert: certificate};

var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
var PORT = 18080;
var SSLPORT = 18081;

httpServer.listen(PORT, function() {
    console.log('HTTP Server is running on: http://localhost:%s', PORT);
});
httpsServer.listen(SSLPORT, function() {
    console.log('HTTPS Server is running on: https://localhost:%s', SSLPORT);
});

app.get('/', function (req, res) {
    res.end("");
})
app.post('/submitform1', function (req, res) {
    //console.log(req.body);
    pool.getConnection(function(err, conn) {
        if (err) console.log(err);
        var keys=[];
        var values=[];
        Object.keys(req.body).forEach(function(key){
            keys.push("`"+key+"`");
            values.push(pool.escape(req.body[key]));
       });
       var sql="INSERT INTO `video_production` ("+keys.join(',')+") VALUES ("+values.join(',')+");";
        conn.query(sql, function (err, rows) {
            if (err) console.log(err);
            conn.release();
        });
    });
    res.end("");
})
app.post('/submitform2', function (req, res) {
    //console.log(req.body);
    pool.getConnection(function(err, conn) {
        if (err) console.log(err);
        var keys=[];
        var values=[];
        Object.keys(req.body).forEach(function(key){
            keys.push("`"+key+"`");
            values.push(pool.escape(req.body[key]));
       });
       var sql="INSERT INTO `brand_design` ("+keys.join(',')+") VALUES ("+values.join(',')+");";
        conn.query(sql, function (err, rows) {
            if (err) console.log(err);
            conn.release();
        });
    });
    res.end("");
})